import java.sql.*;

public class DBInsert {
	
	public static void main (String[] args)
{
	//connection class coming from java.sql can connect to microsoft or oracle etc.
    Connection conn = null;

    try
    {
        String userName = "root";
        String password = "shallu";//if no passwd just double quotes with no space
        //use jdbc 
        String url = "jdbc:mysql://localhost/test";
        //creating an instance for singlton class
       //std syntax
        Class.forName ("com.mysql.jdbc.Driver").newInstance ();
        //using driver we are connected to db
        conn = DriverManager.getConnection (url, userName, password);
     
        System.out.println ("Database connection established");
        //statement object holds query and results temporarily
        //talking to "conn" db
        Statement s = conn.createStatement ();
        //now we can run the queries
        int count;
        int rc=s.executeUpdate ("insert into student values(10,'Manish','manish1234@yahoo.com')");
        System.out.println(rc+" records are updated.");
        s.executeQuery ("SELECT * FROM student");
        //from stmt to resultset obj
        ResultSet rs = s.getResultSet ();
        //as long as there is next record do the while loop
        //don't read the first row as they are the column header
        while (rs.next ())
        {
            int idVal = rs.getInt ("id");
            //first column is 1, second column ie name is 2 or can refer by name of the column
            String nameVal = rs.getString (2);
            String catVal = rs.getString ("email");
            System.out.println (
                    "id = " + idVal
                    + ", name = " + nameVal
                    + ", email = " + catVal);
       //     ++count;
        }
        rs.close ();
        s.close ();

        
        s.close ();
       // System.out.println (count + " rows were inserted");

    }
    catch (Exception e)
    {
        System.err.println ("Cannot connect to database server");
        System.out.println(e);
    }
    finally
    {
        if (conn != null)
        {
            try
            {
                conn.close ();
                System.out.println ("Database connection terminated");
            }
            catch (Exception e) { /* ignore close errors */ }
        }
    }
}

}
